package com.chebaohui.iqs.service.impl.deal.libao.profess;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.fastjson.JSONObject;
import com.chebaohui.iqs.config.CompanyValue;
import com.chebaohui.iqs.entity.base.CarOrder;
import com.chebaohui.iqs.entity.db.DbParam;
import com.chebaohui.iqs.entity.req.CarPermit;
import com.chebaohui.iqs.entity.res.CompanyCarPermit;
import com.chebaohui.iqs.service.impl.deal.libao.sdk.LibaoRequest;
import com.chebaohui.iqs.service.impl.deal.libao.util.ConfirmParamtenGet;
import com.chebaohui.iqs.service.impl.deal.libao.util.LibaoConfig;
import com.chebaohui.iqs.service.impl.deal.libao.util.PublicParam;

/**
 * 利宝车险核保
 * 
 * @author regas_xie
 *
 */
public class LbConfirm {
	/**
	 * log:日志输出
	 */
	private static Log log = LogFactory.getLog(LbConfirm.class);

	/**
	 * 确认投保
	 * 
	 * @param dbParam
	 * @param carPermit
	 * @throws Exception
	 */
	public static CompanyCarPermit confirm(DbParam dbParam, CarPermit carPermit) throws Exception {
		CompanyCarPermit result = new CompanyCarPermit();
		result.setCompanyCode(carPermit.getCompanyCode());
		CarOrder carOrder = carPermit.getCarOrder();
		String orderNo = carPermit.getCarOrder().getOrederNo();

		JSONObject find = JSONObject.parseObject(dbParam.getDatas());

		Map<String, String> map = PublicParam.getParam(orderNo);
		JSONObject json = PublicParam.getParam(map);
		Map<String, String> md5 = md5Map(map);
		md5.put("proposalNo", find.getString("proposalNo"));
		md5.put("mtplProposalNo", find.getString("mtplProposalNo"));
		md5.put("combineFlag", find.getString("combineFlag"));
		md5.put("engineNo", find.getString("engineNo"));
		md5.put("vinNo", find.getString("vinNo"));
		md5.put("modelCode", find.getString("modelCode"));
		json.put("pdk", PublicParam.getPdk(md5));

		ConfirmParamtenGet param = new ConfirmParamtenGet(carPermit);
		/** 配送信息 */
		json.put("tdistributionDto", param.getTdistributionDto());
		/** 车主信息 */
		json.put("tprptCarOwnerDto", param.getTprptCarOwnerDto());
		/** 投保人信息 */
		json.put("tprptApplicantDto", param.getTprptApplicantDto());
		/** 被保险人信息 **/
		json.put("tprptInsuredDto", param.getTprptInsuredDto());
		String str = LibaoRequest.post(
				LibaoConfig.libao_url + "/insure/preminumCommit?access_token=" + map.get("access_token"),
				json.toJSONString());

		log.info(CompanyValue.LIBAO + "核保:" + "\n" + LibaoConfig.libao_url + "/insure/preminumCommit?access_token="
				+ map.get("access_token") + "\n" + "请求参数:" + json.toJSONString() + "\n" + "请求返回:" + str);

		JSONObject hebao = JSONObject.parseObject(str);

		if (!hebao.getBoolean("success"))
			throw new Exception("利宝核保错误:" + hebao.getJSONObject("result").getString("resultMessage"));

		carOrder.setTraInsuranceNo(hebao.getJSONObject("result").getString("proposalMTPLNo"));
		carOrder.setBusInsuranceNo(hebao.getJSONObject("result").getString("proposalNo"));
		result.setCarOrder(carOrder);
		return result;
	}

	/**
	 * 利宝核接口加密字段获取
	 * 
	 * @param map
	 * @return
	 */
	private static Map<String, String> md5Map(Map<String, String> map) {
		Map<String, String> md5 = new HashMap<String, String>();
		md5.put("partnerAccountCode", map.get("partnerAccountCode"));
		md5.put("agreementNo", map.get("agreementNo"));
		md5.put("recordCode", map.get("recordCode"));
		md5.put("flowId", map.get("flowId"));
		return md5;
	}

}
